import BuffUtil from '@/BuffUtil';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
import QS_SmiteHarmmor from './QS_SmiteHarmmor';
import BaseUtil from '@/BaseUtil';
import STimer from '@/STimer';
import SelectCommon from 'code/Core/Common/SelectCommon';
import BuffCommon from 'code/Core/Common/BuffCommon';
/* 虔诚光环 */
export default class QS_PiousAura extends AbilityBase {
    static instance: QS_PiousAura;
    private _providers: unit[] = [];
    buffType: AppBuffType = {
        id: 'buff_QS_PiousAura',
        art: this.art,
        name: this.abilityName,
        ubertip: '你的护甲增加了',
        dur: 2,
        class: '光环',
        targetAttach: 'origin',
        targetArt: 'Abilities\\Spells\\Human\\DevotionAura\\DevotionAura.mdl',
        attribute: {},
    };
    effectBuffType: AppBuffType = {
        id: 'buff_QS_PiousAura_action',
        name: this.abilityName,
        ubertip: '额外增加大量护甲',
        art: this.art,
        dur: 10,
        attribute: {},
        targetAttach: 'overhead',
        targetArt: 'Abilities\\Spells\\Undead\\Possession\\PossessionCaster.mdl',
    };
    constructor(id: string) {
        super(id);
        QS_PiousAura.instance = this;
        this.init();
        this.onLevelChange(this.onLearnSkill.bind(this));
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
        BuffUtil.registerBuffType(this.effectBuffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let us = SelectCommon.getAllyUnitInRange(source, 800);
        for (let u of us) {
            BuffCommon.addBuff({
                target: u,
                caster: source,
                buffTypeId: this.effectBuffType.id,
                ablityId: this.id,
                attribute: {
                    护甲百分比: 0.2 + 0.3 * this.getAbilityLevel(source),
                },
            });
        }
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    private _timer: STimer;
    onLearnSkill(p: { unit: unit; ability: QS_SmiteHarmmor }) {
        if (this._providers.includes(p.unit)) return;
        this._providers.push(p.unit);
        if (this._timer == null) {
            this._timer = BaseUtil.onTimer(1, this.timerEffect.bind(this));
        }
    }
    timerEffect() {
        for (let p of this._providers) {
            if (IsUnitAliveBJ(p)) {
                let lv = this.getAbilityLevel(p);
                let us = SelectCommon.getAllyUnitInRange(p, 800);
                for (let u of us) {
                    let buff = BuffCommon.addBuff({
                        target: u,
                        caster: p,
                        ablityId: this.id,
                        buffTypeId: this.buffType.id,
                    });
                    buff.attribute = {
                        附加护甲: 25 * lv,
                    };
                }
            }
        }
        return true;
    }
}
